package com.yuan.model_community.rest;


import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_community.domain.CommunityUnit;
import com.yuan.model_community.service.CommunityUnitService;
import com.yuan.model_community.service.dto.CommunityUnitDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.utils.PageResult;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Api(tags = "物业便民: 小区单元管理")
@RestController
@RequestMapping("/api/communityUnit")
@RequiredArgsConstructor
public class CommunityUnitController {

    private final CommunityUnitService communityUnitService;

    @ApiOperation("导出/community/unit")
    @GetMapping(value = "/download")
    public void exportUnit(HttpServletResponse response, CommonQueryCriteria criteria) throws IOException {
        communityUnitService.download(communityUnitService.queryAll(criteria), response);
    }

    @GetMapping("/findList")
    @Log("查询/api/community/unit")
    @ApiOperation("查询/api/community/unit")
    public ResponseEntity<PageResult<CommunityUnitDto>> queryUnitList(CommonQueryCriteria criteria, Pageable pageable) {
        PageResult<CommunityUnitDto> communityUnitDtoPageResult = communityUnitService.queryAll(criteria, pageable);
        return new ResponseEntity<>(communityUnitDtoPageResult, HttpStatus.OK);
    }


    @Log("新增/api/community/unit")
    @ApiOperation("新增/api/community/unit")
    @PostMapping("/create")
    public ResponseEntity<Object> createUnit(@Validated @RequestBody CommunityUnit resources) {
        communityUnitService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改/api/community/unit")
    @ApiOperation("修改/api/community/unit")
    @PostMapping("/update")
    public ResponseEntity<Object> updateUnit(@RequestBody CommunityUnit resources) throws Exception {
        communityUnitService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除/api/community/unit")
    @ApiOperation("删除/api/community/unit")
    @PostMapping("/delete")
    public ResponseEntity<Object> deleteUnit(@RequestParam Integer ids) {
        communityUnitService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
